<template>
  <div class="node-wrap">
    <div class="context-warp">
      <el-card>
        <div class="tool-wrap">
          <el-button type="primary" @click="handleAddImage">添加镜像</el-button>
        </div>
        <el-table :data="imageList" height="510px" v-loading="tableLoading">
          <el-table-column prop="id" label="id" width="100"/>
          <el-table-column prop="type" label="节点类型" width="150"/>
          <el-table-column prop="dockerImage" label="镜像版本" />
          <el-table-column prop="createTime" label="创建时间" width="180"/>
        </el-table>
      </el-card>
    </div>

    <el-dialog v-model="addImageFlag"
               title="添加镜像"
               :center="true"
    >
      <el-form :model="imageForm">
        <el-form-item label="节点类型">
          <el-select v-model="imageForm.type" placeholder="请选择节点类型">
            <el-option
              v-for="item in nodeTypeList"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="镜像版本">
          <el-input v-model="imageForm.dockerImage" placeholder="镜像名称:版本号" clearable/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="addImageFlag = false">取 消</el-button>
          <el-button type="primary" @click="handleAddImageConfirm">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import { nodeImageList, NodeImage, AddNodeImage, addImage } from "@/api/NodeApi.ts";
import {NodeType} from "@/enum/NodeType.ts";
import {ElMessage} from "element-plus";

const imageList = ref<Array<NodeImage>>([])
const tableLoading = ref<boolean>(false)
const addImageFlag = ref<boolean>(false)
const imageForm = ref<AddNodeImage>({
  type: '',
  dockerImage: '',
})

const nodeTypeList = ref<Array<string>>([
  NodeType.MAVEN,
  NodeType.NODEJS,
  NodeType.GO,
  NodeType.CMAKE
])

onMounted(() => {
  queryImage()
})

const queryImage = async () => {
  tableLoading.value = true
  const rs = await nodeImageList(null)
  tableLoading.value = false
  imageList.value = rs.data ? rs.data : []
}

const handleAddImage = () => {
  imageForm.value = {
    type: '',
    dockerImage: '',
  }
  addImageFlag.value = true
}

const handleAddImageConfirm = async () => {
  if(!imageForm.value.dockerImage){
    ElMessage.error('镜像版本不能为空')
  }
  const rs = await addImage(imageForm.value)
  if(rs.code === '200'){
    addImageFlag.value = false
    ElMessage.success("添加成功")
    await queryImage()
  }
}
</script>

<style lang="less" scoped>
.node-wrap{
  padding: 50px 50px 50px;
  display: flex;
  justify-content: center;

  .context-warp{
    width: 1200px;
    height: 600px;

    .tool-wrap{
      width: 100%;
      height: 80px;
      padding-bottom: 10px;
      display: flex;
      justify-content: flex-end;
      align-items: end;
    }
  }
}
</style>